import { isUrl } from '../share/utils';

import { CustomEditor } from '../slate';
import { wrapLink } from '../command';

export const withLinks = (editor: CustomEditor) => {
  const { insertText, isInline } = editor;

  editor.isInline = (element) => {
    return element.type === 'link' ? true : isInline(element);
  };

  editor.insertText = (text) => {
    if (text && isUrl(text)) {
      wrapLink(editor, text);
    } else {
      insertText(text);
    }
  };

  return editor;
};
